//
//  GestureViewController.swift
//  EasePods
//
//  Created by 易先生 on 2024/4/30.
//

import UIKit
import JXSegmentedView
import RxDataSources
import RxCocoa
import RxSwift
import CRPEarPhone
import EarbudsFunctions

class GestureViewController: ViewController {
    
    let viewModel: GestureViewModel
    
    init(viewModel: GestureViewModel) {
        self.viewModel = viewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = R.color.common_10_bg_page()
    }
    
    override func makeUI() {
        super.makeUI()
        self.view.addSubview(tableView)
        
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    let fold = PublishRelay<FoldAction>()
    let selectItem = PublishRelay<ModeFunc>()
    
    override func bindViewModel() {
        super.bindViewModel()
        
        let input = GestureViewModel.Input(fold: self.fold.asObservable(),
                                           selectItem: self.selectItem.asObservable())
        let output = self.viewModel.transform(input: input)
        
        let dataSource = RxTableViewSectionedAnimatedDataSource <GestureSectionModel>(animationConfiguration: AnimationConfiguration(
            insertAnimation: .bottom,
            reloadAnimation: .none,
            deleteAnimation: .bottom)) { dataSource, tableView, indexPath, item in
                
                let cell = tableView.dequeueReusableCell(withIdentifier: GestureTableViewCell.reuseIdentifier) as! GestureTableViewCell
                cell.bind(to: item)
                return cell
        }
        output.sections.bind(to: tableView.rx.items(dataSource: dataSource)).disposed(by: rx.disposeBag)
        tableView.rx.setDelegate(self).disposed(by: rx.disposeBag)
        
        tableView.rx.itemSelected.subscribe(onNext: { [weak self] indexPath in
            guard let self = self else { return }
            let section = self.viewModel.dataSource[indexPath.section]
            let cellViewModel = section.items[indexPath.row]
            self.selectItem.accept((mode: section.buttonMode, funcType: cellViewModel.buttonFuncType))
        }).disposed(by: rx.disposeBag)
    }
    

    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .grouped)
        tableView.backgroundColor = R.color.common_10_bg_page()
        tableView.showsVerticalScrollIndicator = false
        tableView.showsHorizontalScrollIndicator = false
        tableView.register(GestureTableViewCell.self, forCellReuseIdentifier: GestureTableViewCell.reuseIdentifier)
        tableView.estimatedSectionHeaderHeight = 70
        tableView.estimatedSectionFooterHeight = 20
        tableView.estimatedRowHeight = 60
        tableView.rowHeight = UITableView.automaticDimension
        tableView.separatorStyle = .none
        return tableView
    }()

}

extension GestureViewController: JXSegmentedListContainerViewListDelegate {
    
    func listView() -> UIView {
        return self.view
    }
}

extension GestureViewController:  UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        
        let section = viewModel.dataSource[section]
        
        let sectionView = GestureaSectionHeadView()
        sectionView.backgroundColor = R.color.common_2_bg_page()
        sectionView.bind(to: section)
        sectionView.unfoldBlock = { [weak self] isOpen in
            self?.fold.accept((mode: section.buttonMode, isOpen: isOpen))
        }
        return sectionView
    }
     
}
